from apps.user.models import User, Config
from utils.Result import Result
from utils.UnSureJsonResponse import UnSureJsonResponse
from utils.hash import get_md5_encryption


def param_register_user(param: dict):
    """
    注册用户参数验证
    """
    username = param.get('username')
    nickname = param.get('nickname')
    password = param.get('password')
    repeat_password = param.get('repeat_password')
    if (username is None and username == ''
            and nickname is None and nickname == ''
            and password is None and password == ''
            and repeat_password is None and repeat_password == ''):
        return False, 1001, '缺少必要参数'
    else:
        if password == repeat_password:
            param.setdefault('password', get_md5_encryption(password))
            if len(User.objects.filter(username=username)) == 0:
                # 不存在此用户
                return True, 200, '操作成功'
            else:
                # 已存在此用户
                return False, 2004, '用户已存在'
        else:
            return False, 2005, '两次输入的密码不一致'


def param_add_user(param: dict, administrators: User):
    """
    管理员添加用户参数验证
    """
    if administrators.role == 1:
        # 管理员
        username = param.get('username')
        nickname = param.get('nickname')
        password = param.get('password')
        if (username is None and username == ''
                and nickname is None and nickname == ''):
            return False, 1001, '缺少必要参数'
        else:
            if len(User.objects.filter(username=username)) == 0:
                # 不存在此用户
                if password is None:
                    # 员工密码为None，使用默认密码
                    try:
                        # 默认密码，加两边密
                        password = Config.objects.get(config_key='员工默认密码')
                        param.setdefault('password', get_md5_encryption(get_md5_encryption(password)))
                        return True, 200, '操作成功'
                    except Config.DoesNotExist:
                        return False, 2101, '员工默认密码配置不存在'
                else:
                    # 存在密码，只需加密一遍
                    param.setdefault('password', get_md5_encryption(password))
                    return True, 200, '操作成功'
            else:
                # 已存在此用户
                return False, 2004, '用户已存在'
    else:
        return False, 1002, '无权限'


def add_user(param, ret, code, message, set_role=False):
    """
    添加用户
    """
    if ret:
        if set_role and param.get('role') == 1:
            user = User.objects.create(username=param.get('username'), nickname=param.get('nickname'),
                                       password=param.get('password'), role=1)
        else:
            user = User.objects.create(username=param.get('username'), nickname=param.get('nickname'),
                                       password=param.get('password'), role=0)
        user.role_int2role_message()
        return UnSureJsonResponse(Result.success(data=user, delete=['is_removed', 'password']))
    else:
        return UnSureJsonResponse(Result.fail(code=code, message=message))


def change_password(param: dict):
    username = param.get('username')
    old_password = param.get('old_password')
    new_password = param.get('new_password')
    repeat_password = param.get('repeat_password')
    if (old_password is None and old_password == ''
            and new_password is None and new_password == ''
            and repeat_password is None and repeat_password == ''):
        return False, 1001, '缺少必要参数'
    else:
        if new_password == repeat_password:
            old_password = get_md5_encryption(old_password)
            new_password = get_md5_encryption(new_password)
            try:
                user = User.objects.get(username=username)
                if old_password == user.password:
                    # 前后密码一致，修改密码
                    user.password = new_password
                    user.save()
                    return True, 200, '操作成功'
                else:
                    # 密码不一致，原密码错误
                    return False, 2006, '密码错误'
            except User.DoesNotExist:
                # 出现异常
                return False, 2002, '用户身份异常'
        else:
            return False, 2005, '两次输入的密码不一致'
